<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="用户名" prop="userName">
        <el-input v-model="queryParams.userName" placeholder="请输入用户名" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="支付金额" prop="paymoney">
        <el-input v-model="queryParams.paymoney" placeholder="请输入支付金额;" clearable
          @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="开始时间" prop="appointedStartTime">
        <el-date-picker clearable v-model="queryParams.appointedStartTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择预约单开始时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="结束时间" prop="appointedOverTime">
        <el-date-picker clearable v-model="queryParams.appointedOverTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择预约单结束时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="服务名称" prop="serviceName">
        <el-input v-model="queryParams.serviceName" placeholder="请输入服务名称" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="支付时间" prop="payTime">
        <el-date-picker clearable v-model="queryParams.payTime" type="date" value-format="YYYY-MM-DD"
          placeholder="请选择支付时间">
        </el-date-picker>
      </el-form-item>
      <el-form-item label="店铺" prop="shopName">
        <el-input v-model="queryParams.shopId" placeholder="请输入店铺ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item label="店员ID" prop="employeeId">
        <el-input v-model="queryParams.employeeId" placeholder="请输入店员ID" clearable @keyup.enter="handleQuery" />
      </el-form-item>
      <el-form-item>

        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>



      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">


        <el-button
          type="primary"
          plain
          icon="plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['shop:appointed:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['shop:appointed:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['shop:appointed:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['shop:appointed:export']"
        >导出</el-button>

      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="appointedList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="预约单ID" align="center" prop="appointedId" />
      <el-table-column label="用户名" align="center" prop="userName" />
      <el-table-column label="支付金额" align="center" prop="paymoney" />
      <el-table-column label="预约单开始时间" align="center" prop="appointedStartTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.appointedStartTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="预约单结束时间" align="center" prop="appointedOverTime" width="160">
        <template #default="scope">
          <span>{{ parseTime(scope.row.appointedOverTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="服务名称" align="center" prop="serviceName" />
      <el-table-column label="状态" align="center" prop="appointedStatus" />
      <el-table-column label="支付时间" align="center" prop="payTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.payTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="店铺" align="center" prop="shopName" />
      <el-table-column label="店员ID" align="center" prop="employeeId" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
            v-hasPermi="['shop:appointed:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
            v-hasPermi="['shop:appointed:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" v-model:page="queryParams.pageNum" v-model:limit="queryParams.pageSize"
      @pagination="getList" />

    <!-- 添加或修改预约对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="appointedRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入用户名" />
        </el-form-item>
        <el-form-item label="支付金额" prop="paymoney">
          <el-input v-model="form.paymoney" placeholder="请输入支付金额" />
        </el-form-item>
        <el-form-item label="开始时间" prop="appointedStartTime">
          <el-date-picker clearable v-model="form.appointedStartTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择预约单开始时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="结束时间" prop="appointedOverTime">
          <el-date-picker clearable v-model="form.appointedOverTime" type="date" value-format="YYYY-MM-DD"
            placeholder="请选择预约单结束时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="服务名称" prop="serviceName">
          <el-input v-model="form.serviceName" placeholder="请输入服务名称" />
        </el-form-item>
        <el-form-item label="支付时间" prop="payTime">
          <el-date-picker clearable v-model="form.payTime" type="date" value-format="YYYY-MM-DD" placeholder="请选择支付时间">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="店铺" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入店铺名" />
        </el-form-item>
        <el-form-item label="店员ID" prop="employeeId">
          <el-input v-model="form.employeeId" placeholder="请输入店员ID" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Appointed">
import { listAppointed, getAppointed, delAppointed, addAppointed, updateAppointed } from "@/api/shop/appointed";
import { listShop } from "@/api/shop/shop"
import useUserStore from '@/store/modules/user'
const { proxy } = getCurrentInstance();
const userStore = useUserStore();
const appointedList = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    userName: null,
    paymoney: null,
    appointedStartTime: null,
    appointedOverTime: null,
    serviceName: null,
    appointedStatus: null,
    payTime: null,
    shopName: null,
    employeeId: null
  },
  queryShopParams: {
    pageNum: 1,
    pageSize: 10,
    shopName: null,
    shopDesc: null,
    shopNumber: null,
    shopStatus: null,
    shopTimeStart: null,
    shopPic: null,
    shopAddress: null,
    phoneNumber: null,
    employeeName: null,
    bossName: null
  },
  rules: {
    userId:[
      { required: true, message: "用户ID不能为空", trigger: "blur" }
    ],
    paymoney: [
      { required: true, message: "支付金额不能为空", trigger: "blur" }
    ],

    appointedStartTime: [
      { required: true, message: "开始时间不能为空", trigger: "blur" }
    ],
    appointedOverTime: [
      { required: true, message: "结束时间不能为空", trigger: "blur" }
    ],

    serviceName:[
      { required: true, message: "服务名称不能为空", trigger: "blur" }
    ],

    payTime:[
      { required: true, message: "支付时间不能为空", trigger: "blur" }
    ],
    shopName:[
      { required: true, message: "店铺名不能为空", trigger: "blur" }
    ],
    employeeId: [
      { required: true, message: "店员ID不能为空", trigger: "blur" }
    ],
  }
});

const { queryParams, queryShopParams, form, rules } = toRefs(data);

/** 查询预约列表 */
function getList() {
  loading.value = true;
  if (userStore.roles[0] == "admin") {
    listAppointed(queryParams.value).then(response => {
      appointedList.value = response.rows;
      total.value = response.total;
      loading.value = false;
    });
  } else if (userStore.roles[0] == "shopManager") {
    // 如果角色为店铺管理员
    queryShopParams.value.employeeName = userStore.name;
    // 查询出管理员管理的店铺
    listShop(queryShopParams.value).then(response => {
      queryParams.value.shopName = response.rows[0].shopName;
      total.value = response.total;
      loading.value = false;
      // 查询出该店铺的所有预约单
      listAppointed(queryParams.value).then(response => {
        appointedList.value = response.rows;
        total.value = response.total;
        loading.value = false;
      });
    }).catch(error => {
      console.log(error);
    });
  } else if (userStore.roles[0] == "boss") {
    queryShopParams.value.bossName = userStore.name;
    // 查询旗下店铺
    listShop(queryShopParams.value).then(response => {
      // 根据旗下店铺查询出所有职工
      for (const shop in response.rows) {
        queryParams.value.shopName = response.rows[shop].shopName;
        total.value = response.total;
        loading.value = false;
        listAppointed(queryParams.value).then(response => {
          // employeeList.value.push(response.rows);
          // 遍历查询结果封装入预约单查询列表
          response.rows.forEach(element => {
            appointedList.value.push(element);
          });
          total.value = response.total;
          loading.value = false;
        });
      }
    }).catch(error => {
      console.log(error);
    });
  }
 
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    appointedId: null,
    userName: null,
    paymoney: null,
    createTime: null,
    appointedStartTime: null,
    appointedOverTime: null,
    serviceName: null,
    appointedStatus: null,
    payTime: null,
    shopName: null,
    employeeId: null
  };
  proxy.resetForm("appointedRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.appointedId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加预约";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _appointedId = row.appointedId || ids.value
  getAppointed(_appointedId).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改预约";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["appointedRef"].validate(valid => {
    if (valid) {
      if (form.value.appointedId != null) {
        updateAppointed(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        console.log(form.value)
        addAppointed(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _appointedIds = row.appointedId || ids.value;
  proxy.$modal.confirm('是否确认删除预约编号为"' + _appointedIds + '"的数据项？').then(function () {
    return delAppointed(_appointedIds);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => { });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('shop/appointed/export', {
    ...queryParams.value
  }, `appointed_${new Date().getTime()}.xlsx`)
}

getList();
</script>
